package marketPlaceAdmin;

import java.rmi.RMISecurityManager;
import java.rmi.Remote;

import net.jini.config.Configuration;
import net.jini.config.ConfigurationProvider;
import net.jini.core.lookup.ServiceID;
import net.jini.discovery.LookupDiscoveryManager;
import net.jini.export.Exporter;
import net.jini.lease.LeaseRenewalManager;
import net.jini.lookup.JoinManager;
import net.jini.lookup.ServiceIDListener;

public class MarketPlaceAdmin implements ServiceIDListener {
	
	private MarketPlaceAdminMarketPlaceProxy fMarketPlaceAdminMarketPlaceImpl;
	private MarketPlaceAdminMarketeerProxy fMarketPlaceAdminMarketeerImpl;
	private int fNrMarketPlaces;
	
	public MarketPlaceAdmin() {
		
		fNrMarketPlaces = 0;
		
		try {
			// Set up 
			Configuration config = ConfigurationProvider.getInstance(new String[] {"marketPlaceAdmin.config"});
			LookupDiscoveryManager discoveryMgr = new LookupDiscoveryManager(new String [] {"JORSEBREGGIE"}, null, null);
			
			// Register MarketPlaceAdminMarketPlace service
			fMarketPlaceAdminMarketPlaceImpl = new MarketPlaceAdminMarketPlaceProxy(this);
			Exporter marketPlaceExporter = (Exporter) config.getEntry("MarketPlaceAdmin", "exporter", Exporter.class);
			Remote marketPlaceProxy = marketPlaceExporter.export(fMarketPlaceAdminMarketPlaceImpl);
			JoinManager marketPlaceJoinMgr = new JoinManager(marketPlaceProxy, null, this, discoveryMgr, new LeaseRenewalManager());
			
			// Register MarketPlaceAdminMarketeer service
			fMarketPlaceAdminMarketeerImpl = new MarketPlaceAdminMarketeerProxy(this);
			Exporter marketeerExporter = (Exporter) config.getEntry("MarketPlaceAdmin", "exporter", Exporter.class);
			Remote marketeerProxy = marketeerExporter.export(fMarketPlaceAdminMarketeerImpl);
			JoinManager marketeerJoinMgr = new JoinManager(marketeerProxy, null, this, discoveryMgr, new LeaseRenewalManager());

		} catch (Exception e) {
			e.printStackTrace();
		}
	
		// Output
		System.out.println("MarketPlaceAdmin started...");
	}

	public void serviceIDNotify(ServiceID serviceID) {
		System.out.println("Service registered with service ID " + serviceID.toString());
	}
	
	public int getNewId() {
		fNrMarketPlaces++;
		return fNrMarketPlaces;
	}
	
	public int getNrMarketPlaces() {
		return fNrMarketPlaces;
	}

}

